#define _CRT_SECURE_NO_WARNINGS 1
#include"Sort.h"

void Print(int* a, int n)
{
	int i = 0;
	for (i = 0;i < n;i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void Swap(int* a, int* b)
{
	int tmp = 0;
	tmp = *a;
	*a = *b;
	*b = tmp;
}

int GetMidi(int* a, int begin, int end)
{
	int midi = (begin + end) / 2;
	if (a[begin] > a[midi])
	{
		if (a[midi] > a[end])
		{
			return midi;
		}
		else if (a[end] > a[begin])
		{
			return begin;
		}
		else
		{
			return end;
		}
	}
	else
	{
		if (a[begin] > a[end])
		{
			return begin;
		}
		else if (a[end] > a[midi])
		{
			return midi;
		}
		else
		{
			return end;
		}
	}
}
void BubbleSort(int* a, int n)
{
	int i = 0;
	for (i = 0;i < n - 1;i++)
	{
		int j = 0;
		for (j = 0;j < n - 1 - i;j++)
		{
			if (a[j] > a[j + 1])
			{
				Swap(&a[j], &a[j + 1]);
			}
		}
	}
}
void SelectSort(int* a, int n)
{
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int maxi = begin, mini = begin;
		int i = 0;
		for (i = begin + 1;i <= end;i++)
		{
			if (a[i] < a[mini])
			{
				mini = i;
			}
			if (a[i] > a[maxi])
			{
				maxi = i;
			}
		}
		Swap(&a[begin], &a[mini]);
		if (maxi == begin)
		{
			maxi = mini;
		}
		Swap(&a[end], &a[maxi]);
		begin++;
		end--;
	}
}
void InsertSort(int* a, int n)
{
	int i = 0;
	for (i = 0;i < n-1;i++)
	{
		int end = i;
		int tmp = a[end + 1];
		while (end >= 0)
		{
			if (a[end] > tmp)
			{
				a[end + 1] = a[end];
				end--;
			}
			else
			{
				break;
			}
		}
		a[end + 1] = tmp;
	}
}
void ShellSort(int* a, int n)
{
	int gap = n;
	while(gap>1)
	{
		gap = gap / 3 + 1;
		int i = 0;
		for (i = 0;i < n - gap; i++)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (a[end] > tmp)
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = tmp;
		}
	}
}
void QuickSortHoare(int* a, int begin, int end)
{
	int left = begin;
	int right = end;
	if (left >= right)
	{
		return;
	}
	int midi = GetMidi(a, begin, end);
	Swap(&a[begin], &a[midi]);
	int keyi = begin;
	while (left < right)
	{
		while (left < right && a[right] >= a[keyi])
		{
			right--;
		}
		while (left < right && a[left] <= a[keyi])
		{
			left++;
		}
		Swap(&a[left], &a[right]);
	}
	Swap(&a[left], &a[keyi]);
	keyi = left;
	QuickSortHoare(a, begin, keyi - 1);
	QuickSortHoare(a, keyi + 1, end);
}
void QuickSortHole(int* a, int begin, int end)
{
	int left = begin;
	int right = end;
	if (begin >= end)
	{
		return;
	}
	int midi = GetMidi(a, begin, end);
	Swap(&a[begin], &a[midi]);
	int key = a[begin];
	int hole = begin;
	while (left < right)
	{
		while (left < right && a[right] >= key)
		{
			right--;
		}
		a[hole] = a[right];
		hole = right;
		while (left < right && a[left] <= key)
		{
			left++;
		}
		a[hole] = a[left];
		hole = left;
	}
	a[hole] = key;
	int keyi = hole;
	QuickSortHole(a, begin, keyi - 1);
	QuickSortHole(a, keyi + 1, end);
}
void QuickSortD(int* a, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int midi = GetMidi(a, begin, end);
	Swap(&a[begin], &a[midi]);
	int keyi = begin;
	int prev = begin;
	int cur = begin + 1;
	while (cur <= end)
	{
		if (a[cur] < a[keyi] && ++prev != cur)
		{
			Swap(&a[cur], &a[prev]);
		}
		cur++;
	}
	Swap(&a[keyi], &a[prev]);
	keyi = prev;
	QuickSortD(a, begin, keyi - 1);
	QuickSortD(a, keyi + 1, end);
}

